import type { BaseResponse } from '../models/up_response';
import { SerialPortFactory } from '../models/factories/SerialPortFactory';
import apiClient from '../utils/axiosInterceptors';

export class SerialMotorService {
  /** 通用 POST 并校验返回状态 */
  private static async postAndCheck(url: string, data?: any): Promise<boolean> {
    const response = await apiClient.post<BaseResponse<null>>(url, data);
    if (response.data.status) {
      return true;
    }
    throw SerialPortFactory.createSerialError(
      response.data.message || 'Unknown error'
    );
  }

  static sendSpeed(
    serial_id: string,
    protocol_id: number,
    rpm: number
  ): Promise<boolean> {
    return this.postAndCheck(
      '/motor/speed',
      SerialPortFactory.createSpeedRequest(serial_id, protocol_id, rpm)
    );
  }
}
